package biz.ezcom.spring.mvc.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import biz.ezcom.spring.mvc.convertor.local.DateConvertor;
import biz.ezcom.spring.mvc.domain.Person;
import biz.ezcom.spring.mvc.service.PersonService;

@Controller
public class ExecuteController {
	private PersonService personService;

	public PersonService getPersonService() {
		return personService;
	}

	@Resource
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	@InitBinder
	private void initBinder(ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Date.class, new DateConvertor());
	}

	@RequestMapping(params = "method=insert")
	public ModelAndView insert(Person person) {
		personService.insert(person);
		return new ModelAndView("execute");
	}

	@RequestMapping(params = "method=delete")
	public ModelAndView delete(int id) {
		personService.deleteById(id);
		return new ModelAndView("execute");
	}

	@RequestMapping(params = "method=update")
	public ModelAndView update(Person person) {
		personService.updateById(person);
		return new ModelAndView("execute");
	}

	@RequestMapping(params = "method=select")
	public ModelAndView select(int id) {
		Person person = personService.selectById(id);
		if(person != null){
			System.out.println(person.getId()+" : "+person.getName()+" : "+person.getBirthday());
		}
		return new ModelAndView("execute");
	}

}